<template>
  <div>
    <!--
        必填：
        action 上传地址，因为用腾讯云 所以用#代替
           file-list 用来管理图片

      list-type 控制样式

      相关钩子
      on-preview 绑定后会显示放大镜, 点击后的弹窗回调需要另外写
      on-remove 删除时触发
      on-change 修改时触发

      上传相关
      before-upload 上传前校验
      http-reuest 替代默认上传行为
    -->
    <el-upload
      action="#"
      :file-list="fileList"
      list-type="picture-card"
      :on-preview="preview"
      :class="{disseble:fileList.length>0}"
      :on-remove="onRemove"
      :on-change="onChange"
      :http-request="upload"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-progress v-if="showProgress" :text-inside="true" :stroke-width="26" :percentage="percentage" style="width:146px" />

    <el-dialog title="预览" :visible="showDialog" @close="showDialog=false">
      <el-row type="flex" justify="center">
        <img :src="imgUrl" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

const cos = new COS({
  SecretId: 'AKIDQggQt51g4B1buwhBOKzNWP1RaS3OSFl0',
  SecretKey: '0jBMGuGL9gxwe6HAm8D1YonX0C8m9F5e'
})
export default {
  data() {
    return {
      fileList: [
        // { url: 'https://img0.baidu.com/it/u=1912568580,484319043&fm=26&fmt=auto&gp=0.jpg' }
      ],
      showDialog: false,
      imgUrl: '',
      percentage: 0, // 进度条的进度
      showProgress: false // 控制进度条显示
    }
  },
  methods: {
    preview(file) {
      this.imgUrl = file.url
      this.showDialog = true
    },
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    onChange(file, fileList) {
      this.fileList = fileList
    },
    beforeUpload(file) {
      // 限制大小
      if (file.size > 2 * 1024 * 1024) {
        this.$message.warning('图片只支持2M以内')
        return false
      }
      // 限制格式
      const whiteList = ['image/jpeg', 'image/png']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('当前只支持 png 和 jpg 格式')
        return false
      }
    },
    upload(params) {
      // console.log('此处放真正的代码')
      this.showProgress = true
      cos.putObject({
        // 储存桶名称
        Bucket: 'hr-1307452106',
        // 地区
        Region: 'ap-guangzhou',
        // 储存文件名
        Key: params.file.name,
        // 固定储存类型不用变
        StorageClass: 'STANDARD',
        // 文件本身
        Body: params.file,
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))

          this.percentage = progressData.percent * 100
        }
      }, (err, data) => {
        // 上传结束的回调
        console.log(err || data)

        if (!err) {
          // 这里是腾讯云异步上传完毕
        // 通知element管理图片的数组修改数据
          this.fileList[0].url = `http://${data.Location}`
          this.fileList[0].status = 'success'

          // 上传完毕 关闭进度条
          setTimeout(() => {
            this.showProgress = false
          }, 1000)
        }
      })
    }

  }
}
</script>

<style lang='scss' scoped>
.disseble{
  //::v-deep  样式穿透
  ::v-deep .el-upload--picture-card{
    display: none;
  }
}
</style>
